package vg.modules.exporters;

import vg.interfaces.ll_services.data_base_service.data.header.GraphModelHeader;
import vg.services.main_manager.VGMainServiceHelper;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.shared.exporters.GraphMLExporter;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GraphMLExporterPlugin implements IPlugin {
	private static final String DEF_LAST_GRAPHML_EXPORT_DIRECTORY = "DEF_LAST_GRAPHML_EXPORT_DIRECTORY";
	
    @Override
	public void install() throws Exception {
        // initialize components
        JMenuItem export2GraphMLMenuItem = new JMenuItem("Export to GraphML");
        VGMainServiceHelper.userInterfaceManager.addMenuItem(export2GraphMLMenuItem, "export");

        // initialize listeners
        export2GraphMLMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                List<GraphModelHeader> graphModelHeaders = VGMainServiceHelper.graphDataBaseManager.getGraphModelHeaders(-1);

                if (graphModelHeaders.size() > 0) {
                    // selecting graph model
                    List<GraphModelHeader> model_headers = new ArrayList<>();
                    String[] possibilities = new String[graphModelHeaders.size()];

                    int i = 0;
                    for (GraphModelHeader buf : graphModelHeaders) {
                        model_headers.add(buf);
                        possibilities[i++] = buf.getName();
                    }

                    Object graph_model_name = JOptionPane.showInputDialog(
                            VGMainServiceHelper.userInterfaceManager.getMainFrame(),
                            "Select graph model:",
                            "Select dialog",
                            JOptionPane.PLAIN_MESSAGE,
                            null,
                            possibilities,
                            possibilities[0]);

                    // getting graph model id
                    GraphModelHeader selecting_model_header = null;
                    for (GraphModelHeader buf : model_headers) {
                        if (buf.getName().equals(graph_model_name)) {
                            selecting_model_header = buf;
                            break;
                        }
                    }

                    // export GraphML
                    if (selecting_model_header != null) {
                        try {
                            JFileChooser fileChooserDialog = new JFileChooser();
                            fileChooserDialog.setFileFilter(new FileFilter() {
                                public String getDescription() {
                                    return "GraphML files(*.graphml)";
                                }

                                public boolean accept(File f) {
                                    return f != null && (f.isDirectory() || f.getName().toLowerCase().endsWith("graphml"));
                                }
                            });
                            fileChooserDialog.setAcceptAllFileFilterUsed(false);
                            String lastDir = VGMainServiceHelper.config.getProperty(DEF_LAST_GRAPHML_EXPORT_DIRECTORY);
                            if(lastDir != null) {
                                fileChooserDialog.setCurrentDirectory(new File(lastDir));
                            }
                            Component parent = VGMainServiceHelper.userInterfaceManager.getMainFrame();
                            if(fileChooserDialog.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
                                VGMainServiceHelper.config.setProperty(DEF_LAST_GRAPHML_EXPORT_DIRECTORY, fileChooserDialog.getCurrentDirectory().getAbsolutePath());
                                File saveFile = fileChooserDialog.getSelectedFile();
                                String fileName = saveFile.getAbsolutePath() + ".graphml";
                                GraphMLExporter.saveGraphML(fileName, selecting_model_header.getId());
                            }
                        } catch (Exception ex) {
                            VGMainServiceHelper.windowMessenger.errorMessage(ex.getMessage(), "Error");
                        }
                    }
                } else {
                    VGMainServiceHelper.windowMessenger.warningMessage("No graph models", "Warning");
                }
            }
        });
	}
}
